<?php
/**
* PHP Template.
*/
class Clubs extends Controller {
	public function __construct() {
			parent::Controller();
			$this->load->model("ClubsModel");
			$this->load->helper("form");
			$this->load->helper("url");
			$this->load->library("errors");
		}
	public function index() {
			$headerData = array(
				"pageTitle"=>"Clubs | Washington Homebrewers Association",
				"pageStyleIndex"=>0, /* Header/Background/Logo pairing */
				"selectedNav"=>"0" /* Selected top level nav */
				);
			$pageData = array();
			$footerData = array();
			$this->load->view("PageTemplate_header.php",$headerData);
			$this->load->view("clubs/index.php",$pageData);
			$this->load->view("PageTemplate_footer.php");
		}
	public function join() {
			$headerData = array(
				"pageTitle"=>"Join a Club | Washington Homebrewers Association",
				"pageStyleIndex"=>0, /* Header/Background/Logo pairing */
				"selectedNav"=>"0" /* Selected top level nav */
				);
			$pageData = array();
			$footerData = array();
			$this->load->view("PageTemplate_header.php",$headerData);
			$this->load->view("clubs/join.php",$pageData);
			$this->load->view("PageTemplate_footer.php");
		}
	public function find() {
			$headerData = array(
				"pageTitle"=>"Find a Club | Clubs | Washington Homebrewers Association",
				"pageStyleIndex"=>1, /* Header/Background/Logo pairing */
				"styleIncludes"=>array("<link rel=\"stylesheet\" type=\"text/css\" href=\"/styles/clubs.css\" />"),
				"selectedNav"=>"0" /* Selected top level nav */
				);
			$pageData = array(
				"clubList"=>$this->ClubsModel->getClubsList()
				);
			$footerData = array();
			$this->load->view("PageTemplate_header.php",$headerData);
			$this->load->view("clubs/find.php",$pageData);
			$this->load->view("PageTemplate_footer.php");
		}
	public function create() {
			$headerData = array(
				"pageTitle"=>"Create A Club | Washington Homebrewers Association",
				"pageStyleIndex"=>0, /* Header/Background/Logo pairing */
				"styleIncludes"=>array("<link rel=\"stylesheet\" type=\"text/css\" href=\"/styles/form.css\" />"),
				"selectedNav"=>"0" /* Selected top level nav */
				);
			$pageData = array(
				"form_button"=>"Create",
				"state"=>$this->db->get("state"),
				"privacy"=>$this->db->get("club_privacy"),
				"location_type"=>$this->db->get("location_type"),
				"form_action"=>"clubs/handleCreate");
			$footerData = array();
			$this->load->view("PageTemplate_header.php",$headerData);
			$this->load->view("clubs/create.php",$pageData);
			$this->load->view("PageTemplate_footer.php");
		}
		/************************** Form Handlers ***************/
		/**
		* Handles form for affiliation
		*/
	public function handleAffiliation() {
		}
	public function handleCreate($isUpdate=false) {
			$this->load->model('ClubsModel'); // Clubs model
			$this->load->library('validation');
			$clubId = $this->ClubsModel->getClubId($_POST["clubname"]); // see if club exist
			$noLocation = isset($_POST["noLocation"]);
			if(!$isUpdate) {
					$rules['clubname'] = "trim|required|xss_clean";
				}
			if(!$isUpdate || ($isUpdate && !empty($_POST["passwd"]))) {
					$rules['passwd'] = "trim|required|matches[passwd2]";
					$rules['passwd2'] = "trim|required|matches[passwd]";
				}
			$rules["club_privacy"] = "required";
			$rules["codeword"] = "trim|required";
		
			if(!$noLocation) {
					$rules["location_type"] = "trim|required";
					$rules["zipcode"] = "trim|required";
					$rules["locname"] = "trim|required";
				}
			$this->validation->set_rules($rules);
		
			if (!$this->validation->run() || $clubId) {
					$fields['clubname']	= 'Club Name';
					$fields['passwd']	= 'Password';
					$fields['passwd2']	= 'Password Confirmation';
					$fields['zipcode']	= 'Postal Code';
					$fields['club_privacy'] = 'Club Privacy';
					$fields['codeword'] = 'Club Privacy';
					$fields['locname'] = 'Location Name';
					$fields['location_type'] = 'Location Type';
					$this->validation->set_fields($fields);
				
					$pageData["state"] = $this->db->get("state");
					$pageData["privacy"] = $this->db->get("club_privacy");
					$pageData["location_type"] = $this->db->get("location_type");
					$pageData["default_values"] = array_merge($_POST);
					if(!$noLocation)
							$pageData["default_values"] = array_merge(array("noLocation"=>"on"));
				
					if($isUpdate) {
							$pageData["form_button"] = "Update";
							$pageData["form_action"] = "clubs/handleUpdate";
							$pageData["isUpdate"] = true;
						} else {
							$pageData["form_button"] = "Create";
							$pageData["form_action"] = "clubs/handleCreate";
						}
					if($clubId)
							$this->phpsession->flashsave('error',Errors::getError(6));
						else
							$this->phpsession->flashsave('error',Errors::getError(5));
					$pageData["default_values"] = array_merge($_POST);
					$headerData = array(
						"pageTitle"=>"Error | Create a club | Washington Homebrewers Association",
						"pageStyleIndex"=>0, /* Header/Background/Logo pairing */
						"styleIncludes"=>array("<link rel=\"stylesheet\" type=\"text/css\" href=\"/styles/form.css\" />"),
						"selectedNav"=>"" /* Selected top level nav */
						);
					$footerData = array();
					$this->load->view("PageTemplate_header.php",$headerData);
					$this->load->view("clubs/create.php",$pageData);
					$this->load->view("PageTemplate_footer.php",$pageData);
				} else { // validation passed
				/*
				[clubname]
				[passwd]
				[passwd2]
				[website]
				[admin] => on 
				[loc_name]
				[address1]
				[address2]
				[city]
				[state]
				[zipcode]
				[club_privacy] => Public 
				[codeword]
				[location_type]
				[mission]
				[welcome_note]
				*/
					$now = date("Y-m-d H:i:s");
					$repId = null; // official rep
					$clubDataArray = array(
						"cName"=>$_POST["clubname"],
						"official_rep_id"=>$repId,
						"website"=>$_POST["website"],
						"created_date"=>$now,
						"enabled"=>1,
						"club_privacy"=>$_POST["club_privacy"],
						"passwd"=>$_POST["passwd"],
						"mission"=>$_POST["mision"],
						"welcome_note"=>$_POST["welcome_note"]
						 );
					$contactDataArray = array(); /*   /*   Not Implemented yet * /
						"phone"=>$_POST["phone"],
						"cell"=>$_POST["cell"],
						"fax"=>$_POST["fax"],
						"email"=>$_POST["email"],
						"best_time"=>$_POST["best_time"],
						"best_days"=>$_POST["best_days"]);
						*/
					if($noLocation) {
							$locDataArray = null;
						} else {
							$locDataArray = array(
								"name"=>$_POST["loc_name"],
								"address1"=>$_POST["address1"],
								"address2"=>$_POST["address2"],
								"city"=>$_POST["city"],
								"state"=>$_POST["state"],
								"zipcode"=>$_POST["zipcode"],
								"location_type"=>$_POST["location_type"],
								"xLat"=>"",
								"yLat"=>""
								);
						}
					if($isUpdate){
							$this->ClubsModel->update($clubDataArray,$contactDataArray,$locDataArray,$now);
							redirect("clubs/update");
						} else {
							$this->ClubsModel->create($clubDataArray,$contactDataArray,$locDataArray,$now);
							redirect("clubs"); // should goto a thank you
						}// end isUpdate
				} // end isValidated
		}
}
?>